/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.proyectos.bean;

import com.proyectos.ejb.DirEjbLocal;
import com.proyectos.model.Directores;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Jose
 */
public class CtrDirectores implements Serializable {

    @EJB
   private DirEjbLocal ejb;
   private String name;
   private List<Directores> lista;
   private Directores activeReg;
   //private DataTable tabla;
   private boolean lnuevo;
   private CtrEntorno oentorno;

    public CtrEntorno getOentorno() {
        return oentorno;
    }

    public void setOentorno(CtrEntorno oentorno) {
        this.oentorno = oentorno;
    }
   
 

    public boolean isLnuevo() {
        return lnuevo;
    }

    public void setLnuevo(boolean lnuevo) {
        this.lnuevo = lnuevo;
    }


    
    public CtrDirectores() {
    }
    public Directores getActiveReg() {
        return activeReg;
    }

    public void setActiveReg(Directores activeReg) {
        this.activeReg = activeReg;
    }

    public void edit(BigDecimal id) {
        try {
            this.lnuevo = false;
            this.activeReg = this.ejb.get(id);
          
            Map<String, Object> options = new HashMap<String, Object>();
            options.put("modal", true);
            options.put("draggable", true);
            options.put("resizable", false);
            options.put("contentHeight", 300);
            options.put("contentWidth",700);
            //options.put("Width",900);
            RequestContext.getCurrentInstance().openDialog("mantDirectores", options, null);
        } catch (Exception ex) {
            Logger.getLogger(CtrDirectores.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
     public void nuevo() {
        try {
            this.lnuevo = true;
            this.activeReg = new Directores();
            this.activeReg.setNcarrera(this.oentorno.getCarrera());
            Map<String, Object> options = new HashMap<String, Object>();
            options.put("modal", true);
            options.put("draggable", true);
            options.put("resizable", false);
            options.put("contentHeight", 300);
            options.put("contentWidth",700);
            RequestContext.getCurrentInstance().openDialog("mantDirectores", options, null);
        } catch (Exception ex) {
            Logger.getLogger(CtrDirectores.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    
    public void save(ActionEvent event) {
       
        try {
           
           if (this.lnuevo) {
                this.ejb.add(activeReg);
            } else {
                this.ejb.save(activeReg);
            }
            this.buscar();
        RequestContext.getCurrentInstance().closeDialog(null);
        } catch (Exception ex) {
            this.addMessage(ex.getMessage());
        }
       
    }
    public void delete(ActionEvent event) {
        try {
            this.ejb.delete(activeReg.getNcodigo());
        } catch (Exception ex) {
            Logger.getLogger(CtrDirectores.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.buscar();
        RequestContext.getCurrentInstance().closeDialog(null);
       
    }
    
    public List<Directores> getLista() {
        return lista;
    }

    public void setLista(List<Directores> lista) {
        this.lista = lista;
    }

    public void addMessage(String summary) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", summary);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void buscar() {
       if(this.oentorno.esDir())
        {   
             TreeMap vparametros = new TreeMap();
             vparametros.put("ccodigo", this.oentorno.director.getCcodigo());   
             vparametros.put("ncarrera", this.oentorno.getCarrera());
             lista = this.ejb.query("Directores.findByCodigo", vparametros);
           
           
        }  
        else
        {   
                
            TreeMap vparametros = new TreeMap();
            vparametros.put("ncarrera", this.oentorno.getCarrera());
            if (name != null && !name.isEmpty()) {
                vparametros.put("cnombre", "%" + name.toUpperCase() + "%");
            } else {
                vparametros.put("cnombre", "%");
            }
            lista = this.ejb.query("Directores.findByName", vparametros);
        }
    }

    public void buscar_todo() {
      
       this.buscar();

    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Directores> getVlista() {
        return this.ejb.query("Directores.findAll",new TreeMap());
        
    }
     public List<Directores> getVlistaCar() {
        TreeMap vparametros = new TreeMap();
        vparametros.put("ncarrera", this.getOentorno().getCarrera());
        return this.ejb.query("Directores.findByAll", vparametros);


    }

}
